<?php
/***********************************/
// World of Warcraft Armory SDK    //
// Licence: GNU GPLv3              //
// Author: Giacomo 'elegos' Furlan //
// File: wowasdk.guild.php         //
/***********************************/

/**
  This class allows you to browse
  your guild's general data
  from the Armory.
*/

class wowasdk_guild {
	private $xmlsheet;
	private $region;
	private $guild_url;
	private $guild_ranks;
	
	/**
	  Constructor of the class.
	  @param $region EU or US
	  @param $server The server's name
	  @param $name The guild's name
	  @param $force_cache TRUE: new data will be downloaded; FALSE: new data will be downloaded only if the cache one is outdated (3 days)
	*/
	function __construct($region,$server,$name, $force_cache = false) {
		$server = explode(" ",$server);
		for($i = 0; $i < count($server); $i++)
			$server[$i] = ucfirst(strtolower($server[$i]));
		$server = implode(" ",$server);
		$server_plus = str_replace(" ","+",$server);
		$fileurl = "guilds/$region/$server/$name.xml";
		$name = urlencode($name);
		
		$this->region = $region;
		
		@mkdir("guilds/$region/$server",0777,true);

		switch($region) {
			case EU:
				$this->guild_url = "http://eu.wowarmory.com/guild-info.xml?";
				break;
			case US:
				$this->guild_url = "http://www.wowarmory.com/guild-info.xml?";
				break;
			default:
				die("No valid region selected.\n");
		}
		
		$replaced_name = str_replace(" ","+",$name);
		$r_string = $this->guild_url."r=$server_plus&n=$replaced_name";
		downloadFileViaCurl($r_string,$fileurl,259200,$force_cache);

		$this->xmlsheet = simplexml_load_file(rawurlencode($fileurl));
	}
	
	/**
	  (Void) Set guild's ranks.
	  input is an array, starting from rank 1 (rank 0 is GM).
	*/
	function setGuildRanks($ranks) {
		$this->guild_ranks = array("Guild Master");
		foreach($ranks as $rank)
			$this->guild_ranks[] = urlencode($rank);
	}
	
	/**
	  (String) Get a guild rank's name from its ID.
	  NOTE: you need to run setGuildRanks first.
	*/
	function getGuildRankName($rank) {
		return $this->guild_ranks[$rank];
	}
	
	/**
	  (String) Battle Group's name.
	*/
	function getBattleGroup() {
		return $this->xmlsheet->guildInfo->guildHeader["battleGroup"];
	}
	
	/**
	  (String) Armory's URL of the guild.
	*/
	function getGuildUrl() {
		return $this->guild_url.$this->xmlsheet->guildInfo->guildHeader["url"];
	}
	
	/**
	  (FACTION_*) Guild's faction.
	*/
	function getFaction() {
		return $this->xmlsheet->guildInfo->guildHeader["faction"];
	}
	
	/**
	  (Integer) Number of members.
	*/
	function getMembersNumber() {
		return $this->xmlsheet->guildInfo->guildHeader["members"];
	}
	
	/**
	  (Array) list of members.
	  $return[n] -> name, gender (GENDER_*), raceid (RACE_*), classid (CLASS_*), level, rankid, url
	*/
	function getMembersList() {
		$return = array();
		foreach($this->xmlsheet->guildInfo->guild->members->character as $char) {
			switch($this->region) {
				case EU:
					$url = "http://eu.wowarmory.com/character-sheet.xml?";
					break;
				case US:
					$url = "http://www.wowarmory.com/character-sheet.xml?";
					break;
			}
			$return[] = array(
				"name" => urlencode($char["name"]),
				"gender" => (int)$char["genderId"],
				"raceid" => (int)$char["raceId"],
				"classid" => (int)$char["classId"],
				"level" => (int)$char["level"],
				"rankid" => (int)$char["rank"],
				"url" => $url.$char["url"],
				
			);
		}
		return $return;
	}
	
	/**
	  (Array) Get the emblem specifics.
	  $return -> background, bordercolor, borderstyle, iconcolor, iconstyle.
	*/
	function getEmblem() {
		return array(
			"background" => $this->xmlsheet->guildInfo->guildHeader->emblem["emblemBackground"],
			"bordercolor" => $this->xmlsheet->guildInfo->guildHeader->emblem["emblemBorderColor"],
			"borderstyle" => $this->xmlsheet->guildInfo->guildHeader->emblem["emblemBorderStyle"],
			"iconcolor" => $this->xmlsheet->guildInfo->guildHeader->emblem["emblemIconColor"],
			"iconstyle" => $this->xmlsheet->guildInfo->guildHeader->emblem["emblemIconStyle"],
		);
	}
}